package com.jeesite.modules.remind.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.financial.entity.billnegotiated.BillNegotiated;
import com.jeesite.modules.financial.entity.billnegotiated.BillNegotiatedDetail;
import com.jeesite.modules.financial.entity.taxclearance.TaxClearance;
import com.jeesite.modules.financial.entity.taxclearance.TaxClearanceDetail;
import com.jeesite.modules.financial.service.billnegotiated.BillNegotiatedService;
import com.jeesite.modules.financial.service.taxclearance.TaxClearanceService;
import com.jeesite.modules.remind.dao.RemindDao;
import com.jeesite.modules.remind.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RemindService {
    @Autowired
    private RemindDao remindDao;
    @Autowired
    private BillNegotiatedService billNegotiatedService;
    @Autowired
    private TaxClearanceService taxClearanceService;

    @Transactional(readOnly = false)
    public List < TcustomerRemind > getCustomerRemindList(TcustomerRemind tcustomerRemind) {
        return remindDao.selectCustomerRemindList(tcustomerRemind);
    }

    @Transactional(readOnly = false)
    public Integer getCustomerRemindCounts(TcustomerRemind tcustomerRemind) {
        return remindDao.selectCustomerRemindCounts(tcustomerRemind);
    }

    @Transactional(readOnly = false)
    public Integer getCustomsDeclaredRemindCounts(CustomsDeclarationRemind customsDeclarationRemind) {
        return remindDao.selectCustomsDeclaredRemindCounts(customsDeclarationRemind);
    }

    @Transactional(readOnly = false)
    public Page < CustomsDeclarationRemind > getCustomsDeclaredRemindListData(CustomsDeclarationRemind customsDeclarationRemind) {
        Page < CustomsDeclarationRemind > page = customsDeclarationRemind.getPage();
//        page.setPageSize(Page.PAGE_SIZE_NOT_PAGING);    // 不分页，返回全部数据。
//        page.setCount(Page.COUNT_NOT_COUNT);            // 不执行Count，不查询总数。
//        page.setCount(Page.COUNT_ONLY_COUNT);            // 只Count数据，不返回数据，通过 page.getCount()获取
        page.setList(remindDao.selectCustomsDeclaredRemindList(customsDeclarationRemind));
        return page;
    }

    @Transactional(readOnly = false)
    public Integer finServiceOrderRemindCounts(FinServiceOrderRemind finServiceOrderRemind) {
        return remindDao.selectFinServiceOrderRemindCounts(finServiceOrderRemind);
    }

    @Transactional(readOnly = false)
    public Page < FinServiceOrderRemind > getFinServiceOrderRemindList(FinServiceOrderRemind finServiceOrderRemind) {
        Page < FinServiceOrderRemind > page = finServiceOrderRemind.getPage();
        page.setList(remindDao.selectFinServiceOrderRemindList(finServiceOrderRemind));
        return page;
    }

    @Transactional(readOnly = false)
    public Integer serviceOrderRemindCounts(ServiceOrderRemind serviceOrderRemind) {
        return remindDao.selectServiceOrderRemindCounts(serviceOrderRemind);
    }
    @Transactional(readOnly = false)
    public Page<ServiceOrderRemind> getServiceOrderRemindList(ServiceOrderRemind serviceOrderRemind) {
        Page <ServiceOrderRemind> page = serviceOrderRemind.getPage();
        page.setList(remindDao.selectServiceOrderRemindList(serviceOrderRemind));
        return page;
    }


    @Transactional(readOnly = false)
    public List <LetterCreditRemind> getLetterCreditRemindList() {
        return remindDao.selectLetterCreditRemindList();
    }

    @Transactional(readOnly = false)
    public Integer getLetterCreditRemindCounts() {
        return remindDao.selectLetterCreditRemindCounts();
    }

    @Transactional(readOnly = false)
    public List <LetterCreditNoRemind> getLetterCreditNoRemindList() {
        return remindDao.selectLetterCreditNoRemindList();
    }

    @Transactional(readOnly = false)
    public Integer getLetterCreditNoRemindCounts() {
        return remindDao.selectLetterCreditNoRemindCounts();
    }

    @Transactional(readOnly = false)
    public List <LadingBillRemind> getLadingBillRemindList() {
        return remindDao.selectLadingBillRemindList();
    }

    @Transactional(readOnly = false)
    public Integer getLadingBillRemindCounts() {
        return remindDao.selectLadingBillRemindCounts();
    }

    /**
     * 供应商信息提醒
     * @return
     */
    @Transactional(readOnly = false)
    public Page<SupplierBusinessReminder> getsupplierBusinessReminder(SupplierBusinessReminder supplierBusinessReminder1) {
        List<SupplierBusinessReminder> supplierBusinessReminders = remindDao.selectsupplierBusinessReminderList();//查询已到期
        for (SupplierBusinessReminder supplierBusinessReminder : supplierBusinessReminders) {
            String count = remindDao.selectsupplierBusinessReminderCounts(supplierBusinessReminder).toString();
            supplierBusinessReminder.setNumberOfLC(count);//开证数量
          Integer days=  (int)DateUtils.getDistanceOfTwoDate(supplierBusinessReminder.getDueDate(),new Date());//到期日期
            supplierBusinessReminder.setDueDays(days>0?days:0);
        }
        Page<SupplierBusinessReminder> page = supplierBusinessReminder1.getPage();
        page.setList(supplierBusinessReminders);
        return page;
    }

    /**
     * 供应链协议提醒列表
     * @return
     */
    @Transactional(readOnly = false)
    public Page<SupplyChainServiceAgreementrRemind> getSupplyChainServiceAgreementrRemindList( SupplyChainServiceAgreementrRemind supplyChainServiceAgreementrRemind) {
        Page<SupplyChainServiceAgreementrRemind> page = supplyChainServiceAgreementrRemind.getPage();
        page.setList(remindDao.selectSupplyChainServiceAgreementrRemindList());
        return page;
    }

    /**
     * 年度汽车销售协议列表
     * @return
     */
    @Transactional(readOnly = false)
    public Page<YearCarSalePurchaseAgreementRemind> getYearCarSalePurchaseAgreementRemindList(YearCarSalePurchaseAgreementRemind yearCarSalePurchaseAgreementRemind) {
        Page<YearCarSalePurchaseAgreementRemind> page = yearCarSalePurchaseAgreementRemind.getPage();
        page.setList(remindDao.selectYearCarSalePurchaseAgreementRemindList());
        return page;
    }
    /**
     * 到单押汇提醒列表
     * @return
     */
    @Transactional(readOnly = false)
    public Page<BillNegotiatedRemind> getBillNegotiatedRemindList(BillNegotiatedRemind billNegotiatedRemind1) {
        List<BillNegotiatedRemind> billNegotiatedReminds = remindDao.selectBillNegotiatedRemindList();
        for (BillNegotiatedRemind billNegotiatedRemind : billNegotiatedReminds) {
            int i=0;
            for (BillNegotiatedDetail billNegotiatedDetail : billNegotiatedService.get(billNegotiatedRemind.getBillNegotiatedId()).getBillNegotiatedDetailList()) {
                if(billNegotiatedDetail.getType().equals("2")) {
                    i++;
                }
                if(billNegotiatedDetail.getBillNegotiatedDetailId().equals(billNegotiatedRemind.getBillNegotiatedDetailId()))
                {
                    billNegotiatedRemind.setCount(i-1);
                    break;
                }
            }
        }
        //单独查询最后一阶段已过期的
        List<BillNegotiated> list = billNegotiatedService.findList(new BillNegotiated())
                .stream().filter(ele->ele.getCustomerActualRepaymentDate()==null).collect(Collectors.toList());//未还款的到单
        for (BillNegotiated billNegotiated : list) {
            BillNegotiated billNegotiated1 = billNegotiatedService.get(billNegotiated);
            List<BillNegotiatedDetail> billNegotiatedDetailList = new ArrayList<>();
            if(billNegotiated1.getBillNegotiatedDetailList().size()>0) {
                billNegotiatedDetailList = billNegotiated1.getBillNegotiatedDetailList().stream().
                        filter(ele -> ele.getType().equals("2")).collect(Collectors.toList());//字表列表

                Collections.reverse(billNegotiatedDetailList);//倒序方便找到最后一阶段
                if (DateUtils.getDistanceOfTwoDate(billNegotiatedDetailList.get(0).getEndDate(), new Date()) > 0)//当前日期已经大于最后一阶段的结束日期
                {
                    BillNegotiatedRemind billNegotiatedRemind = new BillNegotiatedRemind();
                    billNegotiatedRemind.setBillNegotiatedDetailId(billNegotiatedDetailList.get(0).getBillNegotiatedDetailId());
                    billNegotiatedRemind.setBillNegotiatedId(billNegotiated1.getBillNegotiatedId());
                    billNegotiatedRemind.setBillNegotiatedCode(billNegotiated1.getBillNegotiatedCode());
                    if (billNegotiated1.getTcar() != null) {
                        billNegotiatedRemind.setCarName(billNegotiated1.getTcar().getCarName());
                    }
                    if (billNegotiated1.getCustomer() != null) {
                        billNegotiatedRemind.setCustomerName(billNegotiated1.getCustomer().getCustomerName());
                    }
                    if (billNegotiated1.getInvestor() != null) {
                        billNegotiatedRemind.setSupplierName(billNegotiated1.getInvestor().getSupplierName());
                    }
                    billNegotiatedRemind.setStartDate(billNegotiatedDetailList.get(0).getStartDate());
                    billNegotiatedRemind.setEndDate(billNegotiatedDetailList.get(0).getEndDate());
                    billNegotiatedRemind.setCount(billNegotiatedDetailList.size());
                    billNegotiatedReminds.add(billNegotiatedRemind);
                }
            }
        }
        Page<BillNegotiatedRemind> page = billNegotiatedRemind1.getPage();
        page.setList(billNegotiatedReminds);
        return page;
    }
    /**
     * 通关缴税提醒列表
     * @return
     */
    @Transactional(readOnly = false)
    public Page<TaxClearanceRemind> getTaxClearanceRemindList(TaxClearanceRemind taxClearanceRemind1) {
        List<TaxClearanceRemind> taxClearanceReminds = remindDao.selectTaxClearanceRemindList();
        for (TaxClearanceRemind taxClearanceRemind : taxClearanceReminds) {
            int i=0;
            for (TaxClearanceDetail taxClearanceDetail : taxClearanceService.get(taxClearanceRemind.getTaxClearanceId()).getTaxClearanceDetailList()) {
                if(taxClearanceDetail.getType().equals("2")) {
                    i++;
                }
                if(taxClearanceDetail.getTaxClearanceDetailId().equals(taxClearanceRemind.getTaxClearanceDetailId()))
                {
                    taxClearanceRemind.setCount(i-1);
                    break;
                }
            }
        }
        //单独查询最后一阶段已过期的
        List<TaxClearance> list = taxClearanceService.findList(new TaxClearance())
                .stream().filter(ele->ele.getCustomerActualRepaymentDate()==null).collect(Collectors.toList());//未还款的到单
        for (TaxClearance taxClearance : list) {
            TaxClearance taxClearance1 = taxClearanceService.get(taxClearance);
            List<TaxClearanceDetail> taxClearanceDetailList = taxClearance1.getTaxClearanceDetailList().stream().
                    filter(ele->ele.getType().equals("2")).collect(Collectors.toList());//字表列表
            Collections.reverse(taxClearanceDetailList);//倒序方便找到最后一阶段
            if(DateUtils.getDistanceOfTwoDate(taxClearanceDetailList.get(0).getEndDate(),new Date())>0)//当前日期已经大于最后一阶段的结束日期
            {
                TaxClearanceRemind taxClearanceRemind = new TaxClearanceRemind();
                taxClearanceRemind.setTaxClearanceDetailId(taxClearanceDetailList.get(0).getTaxClearanceDetailId());
                taxClearanceRemind.setTaxClearanceId(taxClearance1.getTaxClearanceId());
                taxClearanceRemind.setTaxClearanceCode(taxClearance1.getTaxClearanceCode());
                if (taxClearance1.getTcar() != null) {
                    taxClearanceRemind.setCarName(taxClearance1.getTcar().getCarName());
                }
                if (taxClearance1.getCustomer() != null) {
                    taxClearanceRemind.setCustomerName(taxClearance1.getCustomer().getCustomerName());
                }
                taxClearanceRemind.setStartDate(taxClearanceDetailList.get(0).getStartDate());
                taxClearanceRemind.setEndDate(taxClearanceDetailList.get(0).getEndDate());
                taxClearanceRemind.setCount(taxClearanceDetailList.size());
                taxClearanceReminds.add(taxClearanceRemind);
            }
        }
        Page<TaxClearanceRemind> page = taxClearanceRemind1.getPage();
        page.setList(taxClearanceReminds);
        return page;
    }
    public List<SupplyChainServiceAgreementrRemind> getSupplyChainServiceAgreementrRemindList() {
        return remindDao.selectSupplyChainServiceAgreementrRemindList();
    }
    @Transactional(readOnly = false)
    public Page<ReleaseCarRemind> getReleaseCarRemindListData(ReleaseCarRemind releaseCarRemind) {
        Page <ReleaseCarRemind> page = releaseCarRemind.getPage();
        page.setList(remindDao.selectReleaseCarRemindList(releaseCarRemind));
        return page;
    }
    @Transactional(readOnly = false)
    public Page<ArrivalReminderAgreement> getArrivalReminderAgreementListData(ArrivalReminderAgreement arrivalReminderAgreement) {
        Page <ArrivalReminderAgreement> page = arrivalReminderAgreement.getPage();
        page.setList(remindDao.selectArrivalReminderAgreementList(arrivalReminderAgreement));
        return page;
    }
    @Transactional(readOnly = false)
    public Page<ArRemindAgreeMent> getArRemindListData(ArRemindAgreeMent arRemind) {
        Page <ArRemindAgreeMent> page = arRemind.getPage();
        page.setList(remindDao.selectArRemindList(arRemind));
        return page;
    }
    @Transactional(readOnly = false)
    public Page<LogisticsendRemind> getLogisticsendRemindListData(LogisticsendRemind logisticsendRemind) {
        Page <LogisticsendRemind> page = logisticsendRemind.getPage();
        page.setList(remindDao.selectLogisticsendRemindList(logisticsendRemind));
        return page;
    }
    @Transactional(readOnly = false)
    public Page<RepaymentunpaidRemind> getRepaymentunpaidRemindListData(RepaymentunpaidRemind repaymentunpaidRemind) {
        Page <RepaymentunpaidRemind> page = repaymentunpaidRemind.getPage();
        page.setList(remindDao.selectRepaymentunpaidRemindList());
        return page;
    }
    @Transactional(readOnly = false)
    public Page<OverseasWarehouseRemind> getOverseasWarehouseRemindListData(OverseasWarehouseRemind overseasWarehouseRemind) {
        Page <OverseasWarehouseRemind> page = overseasWarehouseRemind.getPage();
        page.setList(remindDao.selectOverseasWarehouseRemindList(overseasWarehouseRemind));
        return page;
    }
    @Transactional(readOnly = false)
    public Page<LadingBillNoPermitRemind> getLadingBillNoPermitRemindListData(LadingBillNoPermitRemind ladingBillNoPermitRemind) {
        Page <LadingBillNoPermitRemind> page = ladingBillNoPermitRemind.getPage();
        page.setList(remindDao.selectLadingBillNoPermitRemindList(ladingBillNoPermitRemind));
        return page;
    }
    @Transactional(readOnly = false)
    public Page<ArrivalNoDeclareRemind> getArrivalNoDeclareRemindListData(ArrivalNoDeclareRemind arrivalNoDeclareRemind) {
        Page <ArrivalNoDeclareRemind> page = arrivalNoDeclareRemind.getPage();
        page.setList(remindDao.selectArrivalNoDeclareRemindList(arrivalNoDeclareRemind));
        return page;
    }
}
